package org.example;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/9 15:20
 * @DESCRIBE
 */
public class GuessWord {
    public static void main(String[] args) {
        // 随机生成的单词
        String[] words = {"hello", "world", "java", "nice", "good", "love", "hate"};
        Random random = new Random();
        int randomIndex = random.nextInt(words.length);
        String selectWord = words[randomIndex];
        System.out.println("随机生成单词为：" + selectWord);

        // 打印出单词，并用-代替
        char[] chars = selectWord.toCharArray();
        Arrays.fill(chars, '-');
//        System.out.println(Arrays.toString(chars));

        // 打印出单词
        print(chars);

        // 猜单词 确定次数
        int guessTime = 5;
        Scanner scanner = new Scanner(System.in);
        while(true){
            String guessWord = scanner.nextLine();
            char input = guessWord.charAt(0);
            if(selectWord.contains(guessWord)){
                for (int i = 0; i < selectWord.length() ; i++) {
                    if(selectWord.charAt(i) == input){
                        chars[i] = input;
                    }
                }
                print(chars);
            }
            else {
                guessTime--;
                System.out.println("you have "+guessTime+" times left");
            }
            if (!String.valueOf(chars).contains("-")){
                break;
            }
            if (guessTime <= 0) {
                break;
            }

        }
        if (guessTime <= 0) {
            System.out.println("you lose");
        }
        else {
            System.out.println("you win");
        }
    }

    private static void print(char[] chars) {
        for( char e : chars) {
            System.out.print(e);
        }
        System.out.println();
    }
}
